
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第二节 模块化搭建神经网络方法 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section3.3.html" />
    
    
    <link rel="prev" href="section3.1.html" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/section2.1.html">
            
                <a href="../chapter2/section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/section2.2.html">
            
                <a href="../chapter2/section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/section2.3.html">
            
                <a href="../chapter2/section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/section2.4.html">
            
                <a href="../chapter2/section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/section2.5.html">
            
                <a href="../chapter2/section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="section3.1.html">
            
                <a href="section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="section3.2.html">
            
                <a href="section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="section3.3.html">
            
                <a href="section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第二节 模块化搭建神经网络方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E8C;&#x8282;-&#x6A21;&#x5757;&#x5316;&#x642D;&#x5EFA;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x65B9;&#x6CD5;">&#x7B2C;&#x4E8C;&#x8282; &#x6A21;&#x5757;&#x5316;&#x642D;&#x5EFA;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x65B9;&#x6CD5;</h1>
<p>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x516B;&#x80A1;&#x5305;&#x62EC;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x3001;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x3001;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E2D;&#x7528;&#x5230;&#x7684;&#x6B63;&#x5219;&#x5316;&#x3001;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x3001;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x65B9;&#x6CD5;&#x7684;&#x8BBE;&#x7F6E;&#x3001;&#x4EE5;&#x53CA;&#x6D4B;&#x8BD5;&#x6A21;&#x5757;&#x3002;</p>
<ul>
<li>&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;(forward.py)</li>
</ul>
<p>&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5B8C;&#x6210;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x642D;&#x5EFA;&#xFF0C;&#x7ED3;&#x6784;&#x5982;&#x4E0B;:</p>
<pre><code>def forward(x, regularizer):
    w=
    b=
    y=
    return y
def get_weight(shape, regularizer):
def get_bias(shape):
</code></pre><p>&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#x7684;&#x53C2;&#x6570; <code>w</code>&#x548C;&#x504F;&#x7F6E;<code>b</code>&#xFF0C;&#x5B9A;&#x4E49;&#x7531;&#x8F93;&#x5165;&#x5230;&#x8F93;&#x51FA;&#x7684;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;&#x3002;&#x901A;&#x8FC7;&#x5B9A;&#x4E49;&#x51FD;&#x6570;<code>get_weight()</code>&#x5B9E;&#x73B0;&#x5BF9;&#x53C2;&#x6570;<code>w</code>&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x5305;&#x62EC;&#x53C2;&#x6570;<code>w</code>&#x7684;&#x5F62;&#x72B6;&#x548C;&#x662F;&#x5426;&#x6B63;&#x5219;&#x5316;&#x7684;&#x6807;&#x5FD7;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x901A;&#x8FC7;&#x5B9A;&#x4E49;&#x51FD;&#x6570; <code>get_bias()</code>&#x5B9E;&#x73B0;&#x5BF9;&#x504F;&#x7F6E;<code>b</code>&#x7684;&#x8BBE;&#x7F6E;&#x3002;</p>
<ul>
<li>&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;(backword.py)</li>
</ul>
<p>&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5B8C;&#x6210;&#x7F51;&#x7EDC;&#x53C2;&#x6570;&#x7684;&#x8BAD;&#x7EC3;&#xFF0C;&#x7ED3;&#x6784;&#x5982;&#x4E0B;:</p>
<pre><code>def backward(mnist):
  x = tf.placeholder(dtype, shape )
  y_ = tf.placeholder(dtype, shape )
  #&#x5B9A;&#x4E49;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x51FD;&#x6570;
  y = forward()
  global_step =
  loss =
  train_step = tf.train.GradientDescentOptimizer(learning_rate)
  minimize(loss, global_step=global_step)
  #&#x5B9E;&#x4F8B;&#x5316;saver&#x5BF9;&#x8C61;
  saver = tf.train.Saver()
  with tf.Session() as sess:
    #&#x521D;&#x59CB;&#x5316;&#x6240;&#x6709;&#x6A21;&#x578B;&#x53C2;&#x6570;
    tf.initialize_all_variables().run()
    #&#x8BAD;&#x7EC3;&#x6A21;&#x578B;
    for i in range(STEPS):
      sess.run(train_step,feed_dict={x: , y_: })
      if i % &#x8F6E;&#x6570; == 0:
        print()
        saver.save()
</code></pre><p>&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x7528;<code>tf.placeholder(dtype, shape)</code>&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x8BAD;&#x7EC3;&#x6837;&#x672C;<code>x</code>&#x548C;&#x6837;&#x672C;&#x6807;&#x7B7E;<code>y_</code>&#x5360;&#x4F4D;&#xFF0C;&#x51FD;&#x6570;&#x53C2;&#x6570;<code>dtype</code>&#x8868;&#x793A;&#x6570;&#x636E;&#x7684;&#x7C7B;&#x578B;&#xFF0C;<code>shape</code>&#x8868;&#x793A;&#x6570;&#x636E;&#x7684;&#x5F62;&#x72B6;;<code>y</code>&#x8868;&#x793A;&#x5B9A;&#x4E49;&#x7684;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x51FD;&#x6570; <code>forward</code>;<code>loss</code>&#x8868;&#x793A;&#x5B9A;&#x4E49;&#x7684;&#x635F;&#x5931;&#x51FD;&#x6570;&#xFF0C;&#x4E00;&#x822C;&#x4E3A;&#x9884;&#x6D4B;&#x503C;&#x4E0E;&#x6837;&#x672C;&#x6807;&#x7B7E;&#x7684;&#x4EA4;&#x53C9;&#x71B5;(&#x6216;&#x5747;&#x65B9;&#x8BEF;&#x5DEE;)&#x4E0E;&#x6B63;&#x5219;&#x5316;&#x635F;&#x5931;&#x4E4B;&#x548C;;<code>train_step</code>&#x8868;&#x793A;&#x5229;&#x7528;&#x4F18;&#x5316;&#x7B97;&#x6CD5;&#x5BF9;&#x6A21;&#x578B;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4F18;&#x5316;&#xFF0C;&#x5E38;&#x7528;&#x4F18;&#x5316;&#x7B97;&#x6CD5;<code>GradientDescentOptimizer</code>&#x3001;<code>AdamOptimizer</code>&#x3001;<code>MomentumOptimizer</code>&#x7B97;&#x6CD5;&#xFF0C;&#x5728;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#x4F7F;&#x7528;&#x7684;<code>GradientDescentOptimizer</code>&#x4F18;&#x5316;&#x7B97;&#x6CD5;&#x3002;&#x63A5;&#x7740;&#x5B9E;&#x4F8B;&#x5316;<code>saver</code>&#x5BF9;&#x8C61;&#xFF0C;&#x5176;&#x4E2D;&#x5229;&#x7528;<code>tf.initialize_all_variables().run()</code>&#x51FD;&#x6570;&#x5B9E;&#x4F8B;&#x5316;&#x6240;&#x6709;&#x53C2;&#x6570;&#x6A21;&#x578B;&#xFF0C;&#x5229;&#x7528;<code>sess.run()</code>&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x6A21;&#x578B;&#x7684;&#x8BAD;&#x7EC3;&#x4F18;&#x5316;&#x8FC7;&#x7A0B;&#xFF0C;&#x5E76;&#x6BCF;&#x95F4;&#x9694;&#x4E00;&#x5B9A;&#x8F6E;&#x6570;&#x4FDD;&#x5B58;&#x4E00;&#x6B21;&#x6A21;&#x578B;&#x3002;</p>
<ul>
<li>&#x6B63;&#x5219;&#x5316;&#x3001;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x3001;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x65B9;&#x6CD5;&#x7684;&#x8BBE;&#x7F6E;</li>
</ul>
<p>(1) &#x6B63;&#x5219;&#x5316;&#x9879;<code>regularization</code></p>
<p>&#x5F53;&#x5728;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E2D;&#x5373;<code>forward.py</code>&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x8BBE;&#x7F6E;&#x6B63;&#x5219;&#x5316;&#x53C2;&#x6570;<code>regularization</code>&#x4E3A;1 &#x65F6;&#xFF0C;&#x5219;&#x8868;&#x660E;&#x5728;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F18;&#x5316;&#x6A21;&#x578B;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x635F;&#x5931;&#x51FD;&#x6570;&#x4E2D;&#x52A0;&#x5165;&#x6B63;&#x5219;&#x5316;&#x9879;&#x3002;</p>
<p>&#x7ED3;&#x6784;&#x5982;&#x4E0B;:
&#x9996;&#x5148;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5373; forward.py &#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;</p>
<pre><code>if regularizer != None: tf.add_to_collection(&apos;losses&apos;, tf.contrib.layers.l2_regularizer(regularizer)(w))
</code></pre><p>&#x5176;&#x6B21;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5373; backword.py &#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;</p>
<pre><code>ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cem = tf.reduce_mean(ce)
loss = cem + tf.add_n(tf.get_collection(&apos;losses&apos;))
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;<code>tf.nn.sparse_softmax_cross_entropy_with_logits()</code>&#x8868;&#x793A;<code>softmax()</code>&#x51FD;&#x6570;&#x4E0E;&#x4EA4;&#x53C9;&#x71B5;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x3002;</p>
<p>(2) &#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;</p>
<p>&#x5728;&#x8BAD;&#x7EC3;&#x6A21;&#x578B;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x53EF;&#x4EE5;&#x4F7F;&#x6A21;&#x578B;&#x5728;&#x8BAD;&#x7EC3;&#x7684;&#x524D;&#x671F;&#x5FEB;&#x901F;&#x6536;&#x655B;&#x63A5;&#x8FD1;&#x8F83;&#x4F18;&#x89E3;&#xFF0C;&#x53C8;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x6A21;&#x578B;&#x5728;&#x8BAD;&#x7EC3;&#x540E;&#x671F;&#x4E0D;&#x4F1A;&#x6709;&#x592A;&#x5927;&#x6CE2;&#x52A8;&#x3002;</p>
<p>&#x8FD0;&#x7528;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5373; backword.py &#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;:</p>
<pre><code>learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, LEARNING_RATE_STEP, LEARNING_RATE_DECAY, staircase=True)
</code></pre><p>(3) &#x6ED1;&#x52A8;&#x5E73;&#x5747;</p>
<p>&#x5728;&#x6A21;&#x578B;&#x8BAD;&#x7EC3;&#x65F6;&#x5F15;&#x5165;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x53EF;&#x4EE5;&#x4F7F;&#x6A21;&#x578B;&#x5728;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x4E0A;&#x8868;&#x73B0;&#x7684;&#x66F4;&#x52A0;&#x5065;&#x58EE;&#x3002;</p>
<p>&#x9700;&#x8981;&#x5728;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x5373; backword.py &#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;:</p>
<pre><code>ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
  train_op = tf.no_op(name=&apos;train&apos;)
</code></pre><ul>
<li>&#x6D4B;&#x8BD5;&#x8FC7;&#x7A0B;(test.py)</li>
</ul>
<p>&#x5F53;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x8BAD;&#x7EC3;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x4FBF;&#x53EF;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x9A8C;&#x8BC1;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x6027;&#x80FD;&#x3002;&#x7ED3;&#x6784;&#x5982;&#x4E0B;:
&#x9996;&#x5148;&#xFF0C;&#x5236;&#x5B9A;&#x6A21;&#x578B;&#x6D4B;&#x8BD5;&#x51FD;&#x6570;<code>test()</code></p>
<pre><code>def test(mnist):
  with tf.Graph().as_default() as g:
  #&#x7ED9; x y_&#x5360;&#x4F4D;
  x = tf.placeholder(dtype,shape)   
  y_ = tf.placeholder(dtype,shape)
  #&#x524D;&#x5411;&#x4F20;&#x64AD;&#x5F97;&#x5230;&#x9884;&#x6D4B;&#x7ED3;&#x679C;y
  y = mnist_forward.forward(x, None) #&#x524D;&#x5411;&#x4F20;&#x64AD;&#x5F97;&#x5230; y
  #&#x5B9E;&#x4F8B;&#x5316;&#x53EF;&#x8FD8;&#x539F;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x7684; saver
  ema = tf.train.ExponentialMovingAverage(&#x6ED1;&#x52A8;&#x8870;&#x51CF;&#x7387;)
  ema_restore = ema.variables_to_restore()
  saver = tf.train.Saver(ema_restore)
  #&#x8BA1;&#x7B97;&#x6B63;&#x786E;&#x7387;
  correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  while True:
    with tf.Session() as sess:
      #&#x52A0;&#x8F7D;&#x8BAD;&#x7EC3;&#x597D;&#x7684;&#x6A21;&#x578B;
      ckpt = tf.train.get_checkpoint_state(&#x5B58;&#x50A8;&#x8DEF;&#x5F84;)
      #&#x5982;&#x679C;&#x5DF2;&#x6709; ckpt &#x6A21;&#x578B;&#x5219;&#x6062;&#x590D;
      if ckpt and ckpt.model_checkpoint_path:
        #&#x6062;&#x590D;&#x4F1A;&#x8BDD;
        saver.restore(sess, ckpt.model_checkpoint_path)
        #&#x6062;&#x590D;&#x8F6E;&#x6570;
        global_step = ckpt.model_checkpoint_path.split
(&apos;/&apos;)[-1].split(&apos;-&apos;)[-1]
        #&#x8BA1;&#x7B97;&#x51C6;&#x786E;&#x7387;
        accuracy_score = sess.run(accuracy, feed_dict= {x:&#x6D4B;&#x8BD5;&#x6570;&#x636E;, y_:&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x6807;&#x7B7E; })
        # &#x6253;&#x5370;&#x63D0;&#x793A;
        print(&quot;After %s training step(s), test accuracy=%g&quot; % (global_step, accuracy_score))
      #&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6A21;&#x578B;
      else:
        print(&apos;No checkpoint file found&apos;) #&#x6A21;&#x578B;&#x4E0D;&#x5B58;&#x5728;&#x63D0;&#x793A;
        return
</code></pre><p>&#x5176;&#x6B21;&#xFF0C;&#x5236;&#x5B9A; main()&#x51FD;&#x6570;</p>
<pre><code>def main():
#&#x52A0;&#x8F7D;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x96C6;
mnist = input_data.read_data_sets(&quot;./data/&quot;, one_hot=True)
#&#x8C03;&#x7528;&#x5B9A;&#x4E49;&#x597D;&#x7684;&#x6D4B;&#x8BD5;&#x51FD;&#x6570; test()
test(mnist)
if __name__ == &apos;__main__&apos;:
  main()
</code></pre><p>&#x901A;&#x8FC7;&#x5BF9;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x7684;&#x9884;&#x6D4B;&#x5F97;&#x5230;&#x51C6;&#x786E;&#x7387;&#xFF0C;&#x4ECE;&#x800C;&#x5224;&#x65AD;&#x51FA;&#x8BAD;&#x7EC3;&#x51FA;&#x7684;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x7684;&#x6027;&#x80FD;&#x597D;&#x574F;&#x3002;&#x5F53;&#x51C6;&#x786E;&#x7387;&#x4F4E;&#x65F6;&#xFF0C;&#x53EF;&#x80FD;&#x539F;&#x56E0;&#x6709;&#x6A21;&#x578B;&#x9700;&#x8981;&#x6539;&#x8FDB;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x8BAD;&#x7EC3;&#x6570;&#x636E;&#x91CF;&#x592A;&#x5C11;&#x5BFC;&#x81F4;&#x8FC7;&#x62DF;&#x5408;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-09-21 12:13:06
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section3.1.html" class="navigation navigation-prev " aria-label="Previous page: 第一节 MINIST数据">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section3.3.html" class="navigation navigation-next " aria-label="Next page: 第三节 手写数字识别准确率输出">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第二节 模块化搭建神经网络方法","level":"1.4.2","depth":2,"next":{"title":"第三节 手写数字识别准确率输出","level":"1.4.3","depth":2,"path":"chapter3/section3.3.md","ref":"chapter3/section3.3.md","articles":[]},"previous":{"title":"第一节 MINIST数据","level":"1.4.1","depth":2,"path":"chapter3/section3.1.md","ref":"chapter3/section3.1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter3/section3.2.md","mtime":"2018-09-21T04:13:06.225Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

